package com.rampage.multitimers;

import java.io.FileOutputStream;
import java.util.Date;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.rampage.multitimers.bo.TimerConfig;

public class TimerActivity extends Activity {

	private TimerConfig editTimerConfig = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.add_timer);
		
		if (getIntent().getSerializableExtra("timer") != null) {
			editTimerConfig = (TimerConfig)getIntent().getSerializableExtra("timer");
			((TextView)findViewById(R.id.timer_name)).setText(editTimerConfig.name);
			((TextView)findViewById(R.id.timer_seconds)).setText(editTimerConfig.seconds+"");
			((TextView)findViewById(R.id.timer_warning_at)).setText(editTimerConfig.warningAt+"");
			((ToggleButton)findViewById(R.id.voice_alert)).setChecked(editTimerConfig.voiceWarning);
		}

        ((Button)findViewById(R.id.add)).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				TextView name = (TextView)findViewById(R.id.timer_name);
				TextView seconds = (TextView)findViewById(R.id.timer_seconds);
				TextView warningAt = (TextView)findViewById(R.id.timer_warning_at);
				ToggleButton voice = (ToggleButton)findViewById(R.id.voice_alert);
				if (name != null && seconds != null
					&& ! "".equals(name.getText().toString()) && ! "".equals(seconds.getText().toString())
					&& seconds.getText().toString().matches("\\d+")) {
					TimerConfig tc = new TimerConfig();
					tc.name = name.getText().toString();
					tc.seconds = Integer.parseInt(seconds.getText().toString());
					//System.err.println(voice.isChecked());
					tc.voiceWarning = voice.isChecked();
					if (! warningAt.getText().toString().equals("")) {
						tc.warningAt = Integer.parseInt(warningAt.getText().toString());
					} else {
						tc.warningAt = 6;
					}
					if (editTimerConfig != null) {
						int index = -1;
						for (int i=0;i<Main.timerConfigs.size() && index==-1;i++) {
							TimerConfig conf = Main.timerConfigs.get(i);
							if (conf.name.equals(editTimerConfig.name)) {
								index = i;
							}
						}
						if (index != -1) {
							Main.timerConfigs.set(index, tc);
							Main.mTts.synthesizeToFile(tc.name, new HashMap<String, String>(), tc.filename);
						}
					} else {
						//context.getDir("soundfiles", Context.MODE_WORLD_WRITEABLE); 
						tc.filename = "sound"+(int)(Math.random()*100000)+".mp3";
						Main.mTts.synthesizeToFile(tc.name, new HashMap<String, String>(), tc.filename);
						Main.timerConfigs.add(tc);
					}
					Toast.makeText(TimerActivity.this, getResources().getString(R.string.saving_timer), Toast.LENGTH_SHORT).show();
					String FILENAME = "timer_configs.txt";
					try {
						FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
						for (TimerConfig config : Main.timerConfigs) {
							String s = config.toFileSummary() + "\n";
							fos.write(s.getBytes());
						}
						fos.close();
					} catch (Exception e) {
					}
					
					TimerActivity.this.finish();
				}
			}
		});
	}
}
